using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using TextParsing.GraphStructure;

namespace TestXmlSerialization
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{



			Document doc = new Document();
			doc.FileName = "slava.txt";
			doc.TotalSentenceCount = 10;
			doc.TotalTermCount = 20;
			doc.UniqueTermCount = 30;

			Node node = new Node();
			node.Id = 1;
			node.Term = "slava";
			
			Section section = new Section();
			section.TermFrequency = 100;
			section.Type = Section.SectionType.Heading;

			Sentence sentence = new Sentence();
			sentence.NextId = 1234;
			sentence.Number = 90;
			sentence.ConnectingWord = "kaiser";
			sentence.Place = 1;

			section.Sentences.Add(sentence);

			sentence = new Sentence();
			sentence.NextId = 1235;
			sentence.Number = 89;
			sentence.ConnectingWord = null;
			sentence.Place = 1;

			section.Sentences.Add(sentence);

			node.Sections.Add(section);
			doc.Nodes.Add(node);

			XmlSerializer ser = new XmlSerializer(typeof(Document));
			
			StreamWriter writer = new StreamWriter("document.xml",false,System.Text.Encoding.UTF8);

			ser.Serialize(writer,doc);

			writer.Close();

			WriteXml(doc);

		}

		public static void WriteXml(Document doc)
		{
			XmlTextWriter writer = new XmlTextWriter("document2.xml",System.Text.Encoding.UTF8);

			writer.Formatting = System.Xml.Formatting.Indented;
			writer.WriteStartDocument();

			writer.WriteStartElement(Grammar.Document.DocumentElement);

			writer.WriteElementString(Grammar.Document.FileNameElement,doc.FileName);
			writer.WriteElementString(Grammar.Document.TotalTermCountElement,doc.TotalTermCount.ToString());
			writer.WriteElementString(Grammar.Document.UniqueTermCountElement,doc.UniqueTermCount.ToString());
			writer.WriteElementString(Grammar.Document.TotalSentenceCountElement,doc.TotalSentenceCount.ToString());

			WriteNodes(writer,doc.Nodes);

			writer.WriteEndElement();

			writer.WriteEndDocument();
			writer.Close();
		}

		public static void WriteNodes(XmlTextWriter writer,IList nodes)
		{
      IEnumerator nodeEnum = nodes.GetEnumerator();
			while (nodeEnum.MoveNext())
			{
				Node node = (Node) nodeEnum.Current;

				writer.WriteStartElement(Grammar.Node.NodeElement);
				  writer.WriteElementString(Grammar.Node.IdElement,node.Id.ToString());
					writer.WriteElementString(Grammar.Node.TermElement,node.Term);
					writer.WriteElementString(Grammar.Node.TotalTermFrequencyElement,node.TotalTermFrequency.ToString());
          WriteSections(writer,node.Sections);
				writer.WriteEndElement();
			}
		}

		public static void WriteSections(XmlTextWriter writer,IList sections)
		{
			IEnumerator sectionEnum = sections.GetEnumerator();
			while (sectionEnum.MoveNext())
			{
				Section section = (Section) sectionEnum.Current;

				writer.WriteStartElement(Grammar.Section.SectionElement);
				writer.WriteAttributeString(Grammar.SectionType.TextTypeAttribute,GetSectionTypeAttribute(section.Type));
				writer.WriteElementString(Grammar.Section.TermFrequencyElement,section.TermFrequency.ToString());
        WriteSentences(writer,section.Sentences);
				writer.WriteEndElement();
			}
		}

		public static string GetSectionTypeAttribute(Section.SectionType sectionType) 
		{
			switch (sectionType)
			{
				case Section.SectionType.Heading : return Grammar.SectionType.HeadingTypeAttribute;
				case Section.SectionType.Title : return Grammar.SectionType.TitleTypeAttribute;
				case Section.SectionType.Caption : return Grammar.SectionType.CaptionTypeAttribute;
				case Section.SectionType.Text : return Grammar.SectionType.TextTypeAttribute;
				case Section.SectionType.Link : return Grammar.SectionType.LinkTypeAttribute;
				case Section.SectionType.MetaDescription : return Grammar.SectionType.MetaDescriptionTypeAttribute;
				case Section.SectionType.MetaKeyword : return Grammar.SectionType.MataKeywordTypeAttribute;
			}
			return "";
		}

		public static void WriteSentences(XmlTextWriter writer,IList sentences)
		{
			IEnumerator senEnum = sentences.GetEnumerator();
			while (senEnum.MoveNext())
			{
				Sentence sen = (Sentence) senEnum.Current;

				writer.WriteStartElement(Grammar.Sentence.SentenceElement);
				writer.WriteElementString(Grammar.Sentence.NumberElement,sen.Number.ToString());
				writer.WriteElementString(Grammar.Sentence.PlaceElement,sen.Place.ToString());
				if (sen.ConnectingWord != null) writer.WriteElementString(Grammar.Sentence.ConnectingWordElement,sen.ConnectingWord);
				if (sen.NextId >= 0) writer.WriteElementString(Grammar.Sentence.NextIdElement,sen.NextId.ToString());

				writer.WriteEndElement();
			}
		}
	}
}
